﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Notes
{
    public partial class NoteControl : UserControl
    {
        private Note note;

        public Note Data
        {
            get
            {
                return note;
            }
            set
            {
                note = value;
                TitleControl.Text = note.Title;                
                DateControl.Text = note.Creation.ToString();
                TextControl.DocumentText = note.Text;
            }
        }

        public NoteControl(Note note)
        {
            InitializeComponent();

            this.Data = note;
        }

        public void ResizeToFit()
        { 
            //set width first
            Size sz = Parent.ClientSize;
            sz.Width -= 4 * Style.BORDER.Width;
            Size = sz;
           
            // Date control            
            sz = DateControl.GetPreferredSize(new Size(ClientSize.Width, 0));
            sz.Height += Style.BORDER.Height;
            DateControl.Size = sz;
            DateControl.Location = new Point(ClientSize.Width - DateControl.Size.Width, 0);

            // Title control
            TitleControl.Location = new Point(0,0);
            sz = TitleControl.GetPreferredSize(new Size(ClientSize.Width, 0));
            sz.Height += Style.BORDER.Height;
            sz.Width = ClientSize.Width - DateControl.Size.Width;
            TitleControl.Size = sz;

            // Text control
            TextControl.Location = new Point(0,TitleControl.Height);
            sz = TextControl.GetPreferredSize(new Size(ClientSize.Width, 0));
            sz.Height += 2*Style.BORDER.Height;
            sz.Width = ClientSize.Width;
            TextControl.Size = sz;

            sz = Size;
            sz.Height = DateControl.Size.Height + TextControl.Size.Height;
            Size = sz;
        }

        private void TextControl_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (!e.Url.ToString().Contains("about:blank"))
            {
                e.Cancel = true;
                MessageBox.Show(e.Url.ToString() + " " + e.TargetFrameName);
            }
        }
    }
}
